#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

void split(std::string line, std::vector<int> &nums)
{
    int i = 0;
    while (i < line.size())
    {
        int pos = line.find(',', i);
        if (pos == std::string::npos)
        {
            nums.push_back(std::stoi(line.substr(i)));
            break;
        }
        else
        {
            nums.push_back(std::stoi(line.substr(i, pos - i)));
        }
        i = pos + 1;
    }
}

void merge(std::vector<std::vector<int>> &nums, std::vector<int> &merge_vec, int idx, int k)
{
    for (int i = 0; i < nums.size(); i++)
    {
        int minv = nums[i].size() < (idx + k) ? nums[i].size() : (idx + k);
        for (int j = idx; j < minv; j++)
        {
            merge_vec.push_back(nums[i][j]);
        }
    }
}

int main()
{
    int fix_len, line_num;

    std::cin >> fix_len;
    std::cin >> line_num;
    std::cin.ignore();

    std::vector<std::vector<int>> nums_vec;
    int max_size = 0;
    for (int i = 0; i < line_num; i++)
    {
        std::string line;
        std::getline(std::cin, line);
        std::vector<int> nums;
        split(line, nums);
        nums_vec.push_back(nums);
        max_size = nums.size() > max_size ? nums.size() : max_size;
    }

    std::vector<int> res_vec;
    for (int i = 0; i < max_size; i += fix_len)
    {
        merge(nums_vec, res_vec, i, fix_len);
    }

    for (int i = 0; i < res_vec.size() - 1; i++)
    {
        std::cout << res_vec[i] << ",";
    }
    std::cout << res_vec.back() << std::endl;

    return 0;
}